/****************************************************************************
 * Copyright (C) 2012 Maxim Integrated Products, All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated Products
 * shall not be used except as stated in the Maxim Integrated Products
 * Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products retains all ownership rights.
 *
 *     Description: MAXQ serial communications interface driver
 *             $Id: maxq_comm.h 4636 2012-08-01 19:13:17Z jeremy.brodt $
 *
 ********************************************************************************
 */

/** 
* \file
* \brief    Serial port communication driver for use with cstdio
* \details  This file defines the driver API including data types and function
*           prototypes.
* \details  This driver has configurations in maxq_config.h
*/

#ifndef _COMM_INIT_H_
#define _COMM_INIT_H_

#include <stdarg.h>

/***** Definitions *****/
#ifndef COMM_BAUD
/// Default baudrate. This overriden if defined in maxq_config.h
#define COMM_BAUD   115200
#endif

/** 
* \brief    Macro used to calculate the phase register value for the given
*           baudrate
* \details  Baudrate = (PR * F) / (2^23 / 2^(SMOD * 2))
*           SMOD is set to '1'
*           PR = (2^21 * Baudrate) / F
* \warning  This macro is to be calculated during compilation. It MUST include
*           only immediate values
*/        
#define COMM_PR_VALUE   (uint16_t)(((double)0x200000 * COMM_BAUD) / DEFAULT_SYS_CLK_FREQ)
#define COMM_PR_VALUE_INT_OSC   (uint16_t)(((double)0x200000 * COMM_BAUD) / INT_OSC_FREQ)

/// \cond
#ifdef __CROSSWORKS

// include stdio library to use with stings in code 
#include <stdio_c.h>

// use stdio function for stings in code 
#define maxq_printf  printf_c

// Needed for source code compatibility with IAR
#ifndef getchar
#define getchar   __getchar
int __getchar(void);
#endif

// Needed for source code compatibility with IAR
#ifndef putchar
#define putchar   __putchar
int __putchar(int);
#endif

#else

#ifdef COMM_USE_MAXQ_LIB_PRINTF
// Use the MAXQ LIB printf function
void maxq_printf(const char *pcString, ...);
int _maxq_vprintf(const char *pcString, va_list vaArgP, int (*locputchar)(int));
#else
// Use the standard stdio function
#define maxq_printf  printf
#endif

#endif
/// \endcond

/***** Function Prototypes *****/

/**
*   \brief    Initializes and configures the serial port
*   \details  Initializes and configures the serial port (UART0) for
*             communication with a host PC and for use with cstdio. The 
*             desired baudrate should be defined in maxq_config.h.
*   \details  8-bit data, 1 stop bit, no parity
*/
void comm_init(void);

/**
*   \brief    Disables the serial port (UART0)
*/
void comm_deinit(void);

/**
*   \brief    Enables the transmit drivers in the RS-232 transceiver
*   \details  This function deasserts the FORCEOFF input of the MAX3218
*             transceiver, enabling the transmitters.
*   \note     This function only needs to be called in JH3 1-2 is closed.
*             Alternatively, if JH3 2-3 is closed, FORCEOFF is always deasserted.
*/
void comm_tx_enable(void);

/**
*   \brief    Disables the transmit drivers in the RS-232 transceiver
*   \details  This function asserts the FORCEOFF input of the MAX3218
*             transceiver, disabling the transmitters.
*   \note     This function should only be called if JH3 1-2 is closed.
*/
void comm_tx_disable(void);

/**
*   \brief    Enables the receivers in the RS-232 transceiver
*   \details  This function asserts the FORCEON input of the MAX3218
*             transceiver, enabling the receivers.
*   \note     This function only needs to be called in JH1 1-2 is closed.
*             Alternatively, if JH1 2-3 is closed, FORCEON is always asserted.
*/
void comm_rx_enable(void);

/**
*   \brief    Disables the receivers drivers in the RS-232 transceiver
*   \details  This function deasserts the FORCEON input of the MAX3218
*             transceiver, disabling the receivers.
*   \note     This function should only be called if JH1 1-2 is closed.
*/
void comm_rx_disable(void);

#endif /* _COMM_INIT_H_ */
